package cn.lili.modules.promotion.mapper;

import cn.lili.modules.promotion.entity.dos.Coupon;
import cn.lili.modules.promotion.entity.vos.CouponVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 优惠券数据处理层
 *
 * @author Chopper
 * @since 2020/8/21
 */
public interface CouponMapper extends BaseMapper<Coupon> {

    @Select("select c.* from li_coupon c where (coupon_limit_num = 0 or (select count(1) from li_member_coupon mc where mc.member_id = ${memberId}" +
            " and mc.coupon_id = c.id) < coupon_limit_num) and (publish_num = 0 OR (publish_num > 0 and received_num < " +
            "publish_num)) and ${ew.sqlSegment}")
    Page<CouponVO> canReceiveCouponByMemberID(@Param("memberId") String memberId, Page<CouponVO> page,
                                              @Param(Constants.WRAPPER) Wrapper<CouponVO> queryWrapper);

    @Select("select * from li_coupon where (publish_num = 0 OR (publish_num > 0 and received_num < publish_num)) and ${ew.sqlSegment}")
    Page<CouponVO> canReceiveCoupon(Page<CouponVO> page, @Param(Constants.WRAPPER) Wrapper<CouponVO> queryWrapper);

}